package service.impl;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import service.MplanService;
import dao.MplanMapper;
import dao.PlanMapper;
import entity.Mplan;
import entity.Plan;
@Service
public class MplanServiceImpl implements MplanService {
	@Resource
	private MplanMapper mplanMapper;
	@Resource
	private PlanMapper planMapper;
	public boolean add(Mplan mplan,String[] key,String[] val){
		StringBuffer sb1=new StringBuffer();
		for(String k:key){sb1.append(k).append("/");}
		StringBuffer sb2=new StringBuffer();
		for(String v:val){sb2.append(v).append("/");}
		mplan.setKe(sb1.toString());
		mplan.setVa(sb2.toString());
		try {
			mplanMapper.add(mplan);
			Plan plan=new Plan();
			plan.setId(mplan.getPid());
			plan.setMoney(mplan.getMoney());
			planMapper.money(plan);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return true;
	}
	
	public Mplan getone(int pid){
		
		return mplanMapper.one(pid);
	}
	
	public boolean update(Mplan mplan,String[] key,String[] val){
		StringBuffer sb1=new StringBuffer();
		for(String k:key){sb1.append(k).append("/");}
		StringBuffer sb2=new StringBuffer();
		for(String v:val){sb2.append(v).append("/");}
		mplan.setKe(sb1.toString());
		mplan.setVa(sb2.toString());
		try {
			mplanMapper.update(mplan);
			Plan plan=new Plan();
			plan.setId(mplan.getPid());
			plan.setMoney(mplan.getMoney());
			planMapper.money(plan);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

}
